package com.zangs.emqx_expand.web.controller.system;

import com.zangs.emqx_expand.common.annotation.Log;
import com.zangs.emqx_expand.common.core.controller.BaseController;
import com.zangs.emqx_expand.common.core.domain.AjaxResult;
import com.zangs.emqx_expand.common.core.domain.TreeNode;
import com.zangs.emqx_expand.common.core.page.TableDataInfo;
import com.zangs.emqx_expand.common.enums.BusinessType;
import com.zangs.emqx_expand.common.utils.poi.ExcelUtil;
import com.zangs.emqx_expand.system.domain.Api;
import com.zangs.emqx_expand.system.domain.AppUser;
import com.zangs.emqx_expand.system.domain.dto.AuthApiDto;
import com.zangs.emqx_expand.system.service.IApiService;
import com.zangs.emqx_expand.system.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接口密钥管理Controller
 * 
 * 
 * @since 2025-01-20
 */
@RestController
@RequestMapping("/system/api_user")
public class AppUserController extends BaseController
{
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IApiService apiService;
    /**
     * 查询接口密钥管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:api_user:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppUser appUser)
    {
        startPage();
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        return getDataTable(list);
    }

    /**
     * 导出接口密钥管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:api_user:export')")
    @Log(title = "接口密钥管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppUser appUser)
    {
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        util.exportExcel(response, list, "接口密钥管理数据");
    }

    /**
     * 获取接口密钥管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:api_user:query')")
    @GetMapping(value = "/{appUserId}")
    public AjaxResult getInfo(@PathVariable("appUserId") Long appUserId)
    {
        return success(appUserService.selectAppUserByAppUserId(appUserId));
    }

    /**
     * 新增接口密钥管理
     */
    @PreAuthorize("@ss.hasPermi('system:api_user:add')")
    @Log(title = "接口密钥管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppUser appUser)
    {
         if ( appUserService.insertAppUser(appUser)>0){
             appUser.setAppUserId(null);
             return success(appUser);
         }else {
             return error();
         }
    }

    /**
     * 修改接口密钥管理
     */
    @PreAuthorize("@ss.hasPermi('system:api_user:edit')")
    @Log(title = "接口密钥管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppUser appUser)
    {
        return toAjax(appUserService.updateAppUser(appUser));
    }

    /**
     * 删除接口密钥管理
     */
    @PreAuthorize("@ss.hasPermi('system:api_user:remove')")
    @Log(title = "接口密钥管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{appUserIds}")
    public AjaxResult remove(@PathVariable Long[] appUserIds)
    {
        return toAjax(appUserService.deleteAppUserByAppUserIds(appUserIds));
    }


    /**
     * 根据用户编号获取授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:api_user:query')")
    @GetMapping("/authApi/{appUserId}")
    public AjaxResult authApi(@PathVariable("appUserId") Long appUserId)
    {
        AjaxResult ajax = AjaxResult.success();

        AppUser appUser = appUserService.selectAppUserByAppUserId(appUserId);
//        List<Api> apis = apiService.selectAppUserByAppUserId(appUserId);
        List<TreeNode>  treeNode = apiService.selectAuthApi(appUserId);
        ajax.put("appUser", appUser);

        ajax.put("apis",treeNode);
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('system:api_user:edit')")
    @Log(title = "接口密钥管理", businessType = BusinessType.GRANT)
    @PutMapping("/authApi")
    public AjaxResult insertAuthApi(@RequestBody AuthApiDto authApiDto)
    {
        appUserService.insertUserApiAuth(authApiDto.getAppUserId(), authApiDto.getApiIds().toArray(new String[0]));
        return success();
    }
}
